// 用户是否登录接口
var urlGetUserinfo = server_host + '/userweb/getUserInfo.do';
// 调用微信支付
var urlWX = server_host + '/userweb/wxfood/addorderfood.do';
// 查询用户信息
var urlGetUserinfo = server_host + '/userweb/getUserInfo.do';
// 价格减到0后不需要跳转微信支付
var urlSpecialPay = server_host + '/uweb/orderCenter/pay/localSpecialPay.do';
// 红包列表
var urlCouponList = server_host + '/uweb/coupon/food/userCouponList.do';
// 选红包
var ticketValue = getParameterByName('ticketValue');
var userCouponId = getParameterByName('userCouponId');

var cType = getParameterByName('cType');

//参数
var params = getParameterByName('params');

//上个页面标识
var fr = getParameterByName('fr');
// 判断fr是否为空
if(fr){
	sessionStorage.setItem('fr',fr);
	console.log(fr);
}

var foodIds = getParameterByName('foodIds');

// 确认订单
var appModule = angular.module('ConfirmOrder', []);
appModule.controller('ConfirmOrderCtrl', ['$scope', '$http', function($scope, $http) {

	// 将之前的base64码转为json
	$scope.params = base64TojsonObj(params);
	$scope.params.userPhone = sessionStorage.getItem('userPhone');
	// 红包参数
	$scope.ticketValue = ticketValue;
	$scope.userCouponId = userCouponId?userCouponId:0;

	// 判断是否为兑换券
	$scope.cType = cType;

	// 判断用户是否登录
	$scope.loadUserinfo = function() {
		$http.get(urlGetUserinfo).success(function(data, status, headers, config) {
			console.log(data);
			$scope.userInfo = data;
		});
	}

	// 总价计算
	$scope.calcTotal = function() {
		$scope.total = 0;
		for (var i = 0; i < $scope.params.foodList.length; i++) {
			$scope.total = (parseFloat($scope.total) + $scope.params.foodList[i].foodNumber * parseFloat($scope.params.foodList[i].foodPrice)).toFixed(2);
		}
	}

	// 提交
	$scope.submit = function() {
		$http.post(urlWX + '?shopId=' + $scope.params.shopId + '&userCouponId=' + $scope.userCouponId + '&userPhone=' + $scope.params.userPhone + '&ktvOrderNum=' + $scope.params.ktvOrderNum + '&userMsg=' + $scope.params.userMsg + '&tableId=' + $scope.params.tableId + '&foodList=' + angular.toJson($scope.params.foodList)).success(function(data, status, headers, config) {
			console.log(data);
			$scope.submitDetails = data.data;

			// 获取订单号
			$scope.newOrderNum = $scope.submitDetails.orderNum;

			if (data.isUserLogin) {
				if (data.data.success) {

					if(($scope.total - $scope.ticketValue) <= 0){

						$http.post(urlSpecialPay + '?orderNum=' + $scope.newOrderNum).success(function(data, status, headers, config){
							
							if (data.code == 0) {
								location.href = 'ok-pay.html?orderNum=' + $scope.newOrderNum;
							}
						});

						
					}else{
						location.href = 'pay-way.html?orderNum=' + data.data.orderNum + '&payCash=' + $scope.submitDetails.payCash + '&fr=2';
					}

				} else {
					alert(data.data.msg);
				}
			} else {
				alert('请先登录！');
				location.href = 'login.html?params=' + getParameterByName('params') + '&fr=orderfood';
			}
		});

	}

	//返回
	$scope.back = function(){
		if(fr == 'foodinfo'){
			//直接从点餐页面提交，返回到点餐页面
			location.href = 'order-foodinfo.html?params=' + params;
		}else{
			//从点餐页面进入购物车然后提交，返回到购物车页面
			location.href = 'shopping-cart.html?params=' + params;
		}
	}

	//如果sessionStorage中没有userPhone，则调用接口重新获取
	if ($scope.params.userPhone == '' || $scope.params.userPhone == null) {
		$http.get(urlGetUserinfo).success(function(data, status, headers, config) {
			$scope.params.userPhone = data.data.phone;
			sessionStorage.setItem('userPhone', data.data.phone);
		});
	}
	// 获取红包列表
	$scope.loadCouponList = function(){
		$http.get(urlCouponList + '?shopId=' + $scope.params.shopId + '&cash=' + $scope.total + '&foodIds=' + foodIds).success(function(data, status, headers, config) {
			console.log(data);
			$scope.CouponList = data.data;
		});
	}

	// 去选红包
	$scope.GetCoupon = function(){
		location.href = 'use-coupon.html' + '?shopId=' + $scope.params.shopId + '&foodIds=' + foodIds + '&cash=' + $scope.total + '&params=' + getParameterByName('params') + '&fr=' + 1 ;
	}

	// 浮点型计算
	$scope.loadUserinfo();
	$scope.parseFloat = parseFloat;
	$scope.calcTotal();
	$scope.loadCouponList();

}]);